//
//  YKTabBar.m
//  baisi
//
//  Created by 吴高烽 on 16/8/29.
//  Copyright © 2016年 吴高烽. All rights reserved.
//

#import "YKTabBar.h"
#import "YKPublishViewController.h"
#import "YKPostViewController.h"
#import "YKNavigationController.h"
@interface YKTabBar ()
@property (nonatomic ,weak)UIButton *publishBtn;

@end

@implementation YKTabBar
-(instancetype)initWithFrame:(CGRect)frame{
    if (self =[super initWithFrame:frame]) {
        [self setUpAllChilen];
        [self setBackgroundImage:[UIImage imageNamed:@"tabbar-light"]];
    }
    return self;
}
-(void)setUpAllChilen{
    UIButton *publishBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [publishBtn setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal];
    [publishBtn setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateHighlighted];
    publishBtn.size = publishBtn.currentBackgroundImage.size ;
    [publishBtn addTarget:self action:@selector(publishClick) forControlEvents:UIControlEventTouchUpInside];
    self.publishBtn = publishBtn ;
    [self addSubview:publishBtn];
}
-(void)publishClick{
    YKPublishViewController *publishVC = [[YKPublishViewController alloc]init];
//    YKPostViewController *publishVC = [[YKPostViewController alloc]init];
    YKNavigationController *nav = [[YKNavigationController alloc]initWithRootViewController:publishVC];
    YKLog( @"%@",nav);
    [YKKeyWindow.rootViewController presentViewController:nav animated:NO completion:nil];
}
-(void)layoutSubviews{
    [super layoutSubviews];
    CGFloat btnX = 0 ;
    CGFloat btny = 0 ;
    CGFloat btnWidth = self.publishBtn.currentBackgroundImage.size.width;
    CGFloat btnHeight = self.publishBtn.currentBackgroundImage.size.height;
    CGPoint center = CGPointMake(self.width*0.5, self.height*0.5);
    self.publishBtn.center = center ;
    NSInteger indext = 0;
    btnWidth = self.width/5 ;
    btnHeight = self.height ;
    static BOOL added = NO ;
    for (UIControl *btn in self.subviews) {// 判读是否是button类型
        if (![btn isKindOfClass:[UIControl class]] || btn == self.publishBtn) {
            continue ;
        }
        
        btnX = btnWidth*((indext>1)?(indext+1):indext) ;
        btn.frame = CGRectMake(btnX, btny, btnWidth, btnHeight);
        indext++;
        if (added == NO) {
            [btn addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        }
    }
    added = YES ;

}
-(void)buttonClicked:(UIButton*)btn{
    [[NSNotificationCenter defaultCenter] postNotificationName:YKTbaBarDidSelectNotification object:nil userInfo:nil];
    
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(buttonClicked:) name:YKTbaBarDidSelectNotification object:nil];
   
}
@end
